﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tunnel.Tools;

namespace Tunnel.Devices.DeviceEntity
{
    public partial class Fan : DeviceBase
    {
        string zzName;
        string fzName;
        string tzName;
        
        Timer animationTimer = new Timer();

        public string ZzName
        {
            get
            {
                return zzName;
            }

            set
            {
                zzName = value;
            }
        }

        public string FzName
        {
            get
            {
                return fzName;
            }

            set
            {
                fzName = value;
            }
        }

        public string TzName
        {
            get
            {
                return tzName;
            }

            set
            {
                tzName = value;
            }
        }

        public override string DeviceId
        {
            get
            {
                return base.DeviceId;
            }

            set
            {
                base.DeviceId = value;
                ReadParam();
            }
        }

        public override string StatusDescription
        {
            get
            {
                return base.StatusDescription;
            }

            set
            {
                switch(value)
                {
                    case "停止":
                        {
                            animationTimer.Stop();
                            this.Image = ResPic.双风扇90;                            
                        }
                        break;
                    case "正转":
                        {
                            MoveTimerStart();
                        }break;
                    case "反转":
                        {
                            MoveTimerStart();
                        }break;
                    case "故障":
                        {
                            animationTimer.Stop();
                            this.Image = ResPic.双风扇故障;                            
                        }
                        break;
                    case "手动":
                        {
                            animationTimer.Stop();
                            this.Image = ResPic.双风扇故障;                            
                        }
                        break;
                }
                base.StatusDescription = value;
            }
        }

        private void MoveTimerStart()
        {
            if(animationTimer.Enabled==false)
            {
                animationTimer.Start();
            }
            else
            {
                animationTimer.Stop();
                animationTimer.Start();
            }
        }

        private async void ReadParam()
        {
            DataTable dtParamIDTable = await DataDeal.ReadParamAsync(DeviceId);
            foreach (DataRow item in dtParamIDTable.Rows)
            {
                switch (item["ParamStatus"].ToString())
                {
                    case "ZZ": ZzName = item["ParamName"].ToString(); break;
                    case "FZ": FzName = item["ParamName"].ToString(); break;
                    case "TZ": TzName = item["ParamName"].ToString(); break;
                    
                }
            }
        }

        public Fan()
        {
            InitializeComponent();
            animationTimer.Interval = 75;
            animationTimer.Tick += MoveTimer_Tick;
            this.Size = new Size(64, 128);
            this.DoubleClick += Fan_DoubleClick;
        }

        private void Fan_DoubleClick(object sender, EventArgs e)
        {
            FanForm f = new FanForm(this);
            f.ShowDialog();
        }

        int flash = 0;
        private void MoveTimer_Tick(object sender, EventArgs e)
        {
            switch (StatusDescription)
            {
                case "正转":
                    {
                        switch (flash)
                        {
                            case 0: this.Image = ResPic.双风扇30; flash = 1; break;
                            case 1: this.Image = ResPic.双风扇60; flash = 2; break;
                            case 2: this.Image = ResPic.双风扇90; flash = 0; break;
                        }
                    }
                    break;
                case "反转":
                    {
                        switch (flash)
                        {
                            case 0: this.Image = ResPic.双风扇反30; flash = 1; break;
                            case 1: this.Image = ResPic.双风扇反60; flash = 2; break;
                            case 2: this.Image = ResPic.双风扇反90; flash = 0; break;
                        }
                    }
                    break;
                case "停止":
                    {
                        animationTimer.Stop();
                        this.Image = ResPic.双风扇90;                        
                    }
                    break;
            }
        }
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }

        public override void SendCommand(CommandType commandType)
        {
            int seconds = FanTimeControl.getSeconds();
            if (seconds != 0)
            {
                MessageBox.Show($"控制风机间隔过短，请于{seconds}秒后重试", "请不要操作风机过快", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
                return;
            }
            switch (commandType)
            {
                case CommandType.风机正转: SendZZ();break;
                case CommandType.风机反转: SendFZ();break;
                case CommandType.风机停止: SendTZ();break;
            }
            FanTimeControl.LastFanTime = DateTime.Now;
        }

        public void SendZZ()
        {
            DataDeal.SendCommand(DeviceId, ZzName, "1");
            DataDeal.LogDB(this.DeviceId, this.TypeId, "风机正转");
        }
        public void SendFZ()
        {
            DataDeal.SendCommand(DeviceId, FzName, "1");
            DataDeal.LogDB(this.DeviceId, this.TypeId, "风机反转");
        }
        public void SendTZ()
        {
            DataDeal.SendCommand(DeviceId, TzName, "1");
            DataDeal.LogDB(this.DeviceId, this.TypeId, "风机停止");
        }
    }
}
